# @Time : 2020/7/30 14:26
# @Author : Fioman 
# @Phone : 13149920693
import cv2 as cv
import numpy as np


img = cv.imread("cc.bmp")
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)

cv.imshow("Original",img)
T,thres = cv.threshold(gray,127,255,cv.THRESH_BINARY)
image,contours,hierarchy = cv.findContours(thres,cv.RETR_LIST,cv.CHAIN_APPROX_SIMPLE)

# 构造矩形边界框
x,y,w,h = cv.boundingRect(contours[0])

# 使用画矩形函数来画矩形
cv.rectangle(img,(x,y),(x+w,y+h),(0,255,0),thickness=2)

cv.imshow("BoundingRect",img)
cv.waitKey(0)
cv.destroyAllWindows()